<?php defined('SYSPATH') or die('No direct script access.');

class Controller_PoS extends Controller {
	
	private $ss;
	private $message;
	private $thesystems;
	private $pos_module;
	private $mydatetime;
	
	public function __construct(Request $request, Response $response)
	{
		parent::__construct($request, $response);
		
		$language		= Helper_LanguageSupport::factory();
		
		$this->ss					= Session::instance();
		$this->thesystems	= Helper_theSystems::factory();
		$this->mydatetime	= Helper_MyDateTime::factory();
		$this->message		= Language_Message::factory();
		
		// Checking for Point of Sales Module
		$this->pos_module = $this->thesystems->system_auth('system_accounting_pointofsales');
		
		if(!is_null($this->thesystems->system_auth('system_multilanguage'))) {
			// Change the Language
			$lang = Arr::get($_GET,'lang');
			if(isset($lang))
			{
				$language->set_new_language($lang);
				$r = Request::current();
				$r->redirect('pos/'.$this->request->action().URL::query(array('lang'=>NULL)));
			}
		}
		
		// Setting Starting ID for The Language Support
		// If langid is NULL, Then Set the langid to EN
		$langid = $this->ss->get('langid');
		
		if(is_null($langid))
		{
			$this->ss->set('langid','EN');
		} else {
			$this->ss->set('langid',$langid);
		}
		
	}
	
	public function before()
	{
		$loginsupport	= Helper_LoginSupport::factory();

		if($this->request->action() != 'unexist') {
			if(is_null($this->pos_module)) {
				$r = Request::current();
				$r->redirect('pos/unexist');
			} else {
				// Check if User Already Logged In
				if(!(($this->request->action() == 'login') || ($this->request->action() == 'captcha')))
				{
					if($this->ss->get('logid') == '' || $this->ss->get('logstatus') == '')
					{
						if($this->request->action() != 'index')
						{
							$this->message->store_message('not_login');
						}
						
						$loginsupport->logout();

						$r = Request::current();
						$r->redirect('pos/login');
					} else {
						if($this->ss->get('logstatus') > 900)
						{
							$this->message->store_message('not_allowed');
							
							$loginsupport->logout();

							$r = Request::current();
							$r->redirect('pos/login');
						}
					}
				}
			}
		}
	}
	public function action_captcha()
	{
	  $this->response->body(Captcha::render());
	}
	
	public function action_login()
	{
		$html         = NULL;
		$loginresult  = NULL;
		$loginsupport = Helper_LoginSupport::factory();
		$thesystems   = Helper_thesystems::factory();
		$command      = Arr::get($_REQUEST,'command');
		$captcha      = $thesystems->system_auth('system_captcha');

		switch ($command)
		{
		case "login":
				if($captcha != NULL) {
					$post_captcha = Arr::get($_POST,'captchacode');
					$ss_captcha   = Session::instance()->get('captcha_code');
				} else {
					$post_captcha = 1;
					$ss_captcha   = 1;
				}
				  if ($post_captcha == $ss_captcha)
				  { 
					  $this->ss->delete('captcha_code');
					  $cszusername	= Arr::get($_POST, 'cszusername');
					  $cszpassword	= Arr::get($_POST, 'cszpassword');
					  
					  $loginresult = $loginsupport->login($cszusername,$cszpassword);
					  
					  if(!empty($loginresult['logid']))
					  {
						  $this->ss->set('logid',$loginresult['logid']);
						  $this->ss->set('logstatus',$loginresult['logstatus']);
						  
							$this->message->store_message('login_success');
						  $r = Request::current();
						  if($loginresult['logstatus'] == '900') 
							{
								$r->redirect('pos/invoice');
							} else {
								$r->redirect('pos/index');
							}
					  } else {
						  $this->message->store_message('login_fails');
						  $r = Request::current();
						  $r->redirect('pos/login');
					  }
				  } else {
						$this->message->store_message('wrong_captcha');
						$r = Request::current();
						$r->redirect('pos/login');
				  }
				  break;
		}

		$content['title']		= 'Inspiring Point of Sales Login Page';
		$content['content']	= View::factory('inc/login')
			->set('loginresult',$loginresult)
			->render();
		
		$html = View::factory('v_pos_templ')->set('content',$content)->render();
		$this->response->body($html);
	}
	
	public function action_logout()
	{
		$loginsupport	= Helper_LoginSupport::factory();
		
		$loginsupport->logout();
		
		$this->message->store_message('logout_success');
		$r = Request::current();
		$r->redirect('pos/login');
	}
	
	public function action_index()
	{
		$dashboard		= Module_POSHome_Dashboard::factory();
		
		$html = View::factory('v_pos_templ')->set('content',$dashboard->showdashboard())->render();
		$this->response->body($html);
	}
  

//#################################### User Administrator ################################################
	public function action_karyawan()
	{
		$karyawan = Module_users_karyawan::factory();
		
		$command  = Arr::get($_REQUEST,'command');
		$id       = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$karyawan->showkaryawan($id, $command))->render();
		$this->response->body($html);
	}
	
	public function action_pelanggan()
	{
		$users   = Module_Users_pelanggan::factory();
		
		$command = Arr::get($_REQUEST,'command');
		$id      = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$users->pelanggan($id, $command))->render();
		$this->response->body($html);
	}

	public function action_mastertruk()
	{
		$mastertruk = Module_mastertruk_mastertruk::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$mastertruk->mastertruk($id, $command))->render();
		$this->response->body($html);
	}

	public function action_mastersukucadang()
	{
		$mastersukucadang = Module_mastersukucadang_mastersukucadang::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$mastersukucadang->mastersukucadang($id, $command))->render();
		$this->response->body($html);
	}

	public function action_posisi()
	{
		$masterposisi = Module_masterposisi_masterposisi::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$masterposisi->masterposisi($id, $command))->render();
		$this->response->body($html);
	}

	public function action_vo()
	{
		$vo = Module_vo_vo::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$vo->vo($id, $command))->render();
		$this->response->body($html);
	}

	public function action_suratjalan()
	{
		$suratjalan = Module_suratjalan_suratjalan::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$suratjalan->suratjalan($id, $command))->render();
		$this->response->body($html);
	}

	public function action_sttb()
	{
		$sttb = Module_sttb_sttb::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$sttb->sttb($id, $command))->render();
		$this->response->body($html);
	}

	public function action_pembayaran()
	{
		$pembayaran = Module_pembayaran_pembayaran::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$pembayaran->pembayaran($id, $command))->render();
		$this->response->body($html);
	}

	public function action_sppsc()
	{
		$sppsc = Module_sppsc_sppsc::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$sppsc->sppsc($id, $command))->render();
		$this->response->body($html);
	}

	public function action_spsc()
	{
		$spsc = Module_spsc_spsc::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$spsc->spsc($id, $command))->render();
		$this->response->body($html);
	}

	public function action_po()
	{
		$po = Module_po_po::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$po->po($id, $command))->render();
		$this->response->body($html);
	}

	public function action_spkp()
	{
		$spkp = Module_spkp_spkp::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$spkp->spkp($id, $command))->render();
		$this->response->body($html);
	}

	public function action_suratpermintaansc()
	{
		$suratpermintaansc = Module_suratpermintaansc_suratpermintaansc::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$suratpermintaansc->suratpermintaansc($id, $command))->render();
		$this->response->body($html);
	}

	public function action_perbaikan()
	{
		$perbaikan = Module_perbaikan_perbaikan::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$perbaikan->perbaikan($id, $command))->render();
		$this->response->body($html);
	}

	public function action_penyelesaiankerja()
	{
		$penyelesaiankerja = Module_penyelesaiankerja_penyelesaiankerja::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$penyelesaiankerja->penyelesaiankerja($id, $command))->render();
		$this->response->body($html);
	}

	public function action_pengeluaransukucadang()
	{
		$pengeluaransukucadang = Module_pengeluaransukucadang_pengeluaransukucadang::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$pengeluaransukucadang->pengeluaransukucadang($id, $command))->render();
		$this->response->body($html);
	}

	public function action_penerimaanbarangmasuk()
	{
		$penerimaanbarangmasuk = Module_penerimaanbarangmasuk_penerimaanbarangmasuk::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$penerimaanbarangmasuk->penerimaanbarangmasuk($id, $command))->render();
		$this->response->body($html);
	}

	public function action_salesorder()
	{
		$salesorder = Module_salesorder_salesorder::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$salesorder->salesorder($id, $command))->render();
		$this->response->body($html);
	}

	public function action_perjanjiankontrak()
	{
		$perjanjiankontrak = Module_perjanjiankontrak_perjanjiankontrak::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$perjanjiankontrak->perjanjiankontrak($id, $command))->render();
		$this->response->body($html);
	}

	/*tambahan start*/
	public function action_jeniskiriman()
	{
		$jeniskiriman = Module_jeniskiriman_jeniskiriman::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$jeniskiriman->jeniskiriman($id, $command))->render();
		$this->response->body($html);
	}
	public function action_jenissukucadang()
	{
		$jenissukucadang = Module_jenissukucadang_jenissukucadang::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$jenissukucadang->jenissukucadang($id, $command))->render();
		$this->response->body($html);
	}
	public function action_tipesukucadang()
	{
		$tipesukucadang = Module_tipesukucadang_tipesukucadang::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$tipesukucadang->tipesukucadang($id, $command))->render();
		$this->response->body($html);
	}
	public function action_jenistruk()
	{
		$jenistruk = Module_jenistruk_jenistruk::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$jenistruk->jenistruk($id, $command))->render();
		$this->response->body($html);
	}
	public function action_kota()
	{
		$kota = Module_kota_kota::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$kota->kota($id, $command))->render();
		$this->response->body($html);
	}
	public function action_mereksukucadang()
	{
		$mereksukucadang = Module_mereksukucadang_mereksukucadang::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$mereksukucadang->mereksukucadang($id, $command))->render();
		$this->response->body($html);
	}
	public function action_merektruk()
	{
		$merektruk = Module_merektruk_merektruk::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory('v_pos_templ')->set('content',$merektruk->merektruk($id, $command))->render();
		$this->response->body($html);
	}
	/*laporan*/
	public function action_lappenyewaan()
	{
		$lappenyewaan = Module_lappenyewaan_lappenyewaan::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lappenyewaan->lappenyewaan($id, $command))->render();
		$this->response->body($html);
	}
	public function action_lappengeluaransukucadang()
	{
		$lappengeluaransukucadang = Module_lappengeluaransukucadang_lappengeluaransukucadang::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lappengeluaransukucadang->lappengeluaransukucadang($id, $command))->render();
		$this->response->body($html);
	}
	public function action_lapperbaikan()
	{
		$lapperbaikan = Module_lapperbaikan_lapperbaikan::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lapperbaikan->lapperbaikan($id, $command))->render();
		$this->response->body($html);
	}
	public function action_lappembelian()
	{
		$lappembelian = Module_lappembelian_lappembelian::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lappembelian->lappembelian($id, $command))->render();
		$this->response->body($html);
	}
	public function action_lappenerimaansc()
	{
		$lappenerimaansc = Module_lappenerimaansc_lappenerimaansc::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lappenerimaansc->lappenerimaansc($id, $command))->render();
		$this->response->body($html);
	}
	public function action_lappengeluaransc()
	{
		$lappengeluaransc = Module_lappengeluaransc_lappengeluaransc::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lappengeluaransc->lappengeluaransc($id, $command))->render();
		$this->response->body($html);
	}
	public function action_lappenyelesaiankerja()
	{
		$lappenyelesaiankerja = Module_lappenyelesaiankerja_lappenyelesaiankerja::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lappenyelesaiankerja->lappenyelesaiankerja($id, $command))->render();
		$this->response->body($html);
	}
	public function action_lapkeuangan()
	{
		$lapkeuangan = Module_lapkeuangan_lapkeuangan::factory();
		
		$command    = Arr::get($_REQUEST,'command');
		$view    = Arr::get($_REQUEST,'view');
		$id         = Arr::get($_GET, 'id');
	
		$html = View::factory(($view == 'print' ? 'p_pos_templ' : 'v_pos_templ'))->set('content',$lapkeuangan->lapkeuangan($id, $command))->render();
		$this->response->body($html);
	}

///####################################report########################################//
	
	public function action_print()
	{
		$command = Arr::get($_REQUEST,'command');
		$table   = Arr::get($_REQUEST,'table');
		$id      = Arr::get($_REQUEST,'id');
		switch ($command)
			{
			case "print_perjanjian_kontrak":
			  $html = View::factory('print/print_perjanjian_kontrak')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_sales_order":
			  $html = View::factory('print/print_sales_order')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_voucher_order":
			  $html = View::factory('print/print_voucher_order')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_surat_jalan":
			  $html = View::factory('print/print_surat_jalan')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_tanda_terima_barang":
			  $html = View::factory('print/print_tanda_terima_barang')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_pembayaran":
			  $html = View::factory('print/print_pembayaran')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_permintaan_pembelian_sukucadang":
			  $html = View::factory('print/print_permintaan_pembelian_sukucadang')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_pembelian_sukucadang":
			  $html = View::factory('print/print_pembelian_sukucadang')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_purchase_order":
			  $html = View::factory('print/print_purchase_order')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_perintah_kerja_perbaikan":
			  $html = View::factory('print/print_perintah_kerja_perbaikan')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_permintaan_sukucadang":
			  $html = View::factory('print/print_permintaan_sukucadang')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_perbaikan":
			  $html = View::factory('print/print_perbaikan')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_penyelesaian_kerja":
			  $html = View::factory('print/print_penyelesaian_kerja')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_pengeluaran_sukucadang":
			  $html = View::factory('print/print_pengeluaran_sukucadang')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			case "print_penerimaan_barang_masuk":
			  $html = View::factory('print/print_penerimaan_barang_masuk')
				  ->set('table',$table)
				  ->set('id',$id)
				  ->render();
			  break;
			
		}
		
		$this->response->body($html);
	}


	
//#################################### Module Dont Exist ##############################################
	public function action_unexist()
	{
		$html = View::factory('unavailable')->render();
		$this->response->body($html);
	}






 
} // End Welcome